﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Text;
using System.Diagnostics;
using BigMailer.Client;
using BigMailer.Client.Services;

namespace BigMailer
{
	public partial class ExistingFileControl : UserControl
	{
		public event EventHandler Deleted;

		ExistingFile file;

		public ExistingFileControl(ExistingFile file)
		{
			InitializeComponent();
			this.file = file;
			tbDate.Text = GetDateText();

			fileLink.Inlines.Clear();
			fileLink.ContentStart.InsertTextInRun( file.Name );
			tbSize.Text = GetFileSizeText();
			UpdateUi();
		}

		private string GetFileSizeText()
		{
			if ( file.Size < 1000 * 1024 )
			{
				return (file.Size / 1024.0).ToString( "N0" ) + " KB";
			}
			else
			{
				return (file.Size / 1024.0 / 1000).ToString( "N0" ) + " MB";
			}
		}

		private string GetDateText()
		{
			string dateText = file.Created.ToShortDateString();

			StringBuilder sb = new StringBuilder();
			foreach ( string part in dateText.Split('/') )
			{
				sb.Append( part.PadLeft( 2, '0' ) );
				if (part.Length < 4)
				{
					sb.Append( "/" );
				}
			}

			return sb.ToString();
		}

		private void viewLink_Click(object sender, RoutedEventArgs e)
		{
			UiCore.ShowFinalLink( file.Url );
		}

		private void deleteLink_Click(object sender, RoutedEventArgs e)
		{
			if (MainWindow.WarnOnDelete )
			{
				string text = "Are you sure you want to delete " + file.Name + "?";
				string cap = "Confirm Delete";
				if ( MessageBox.Show( text, cap, MessageBoxButton.YesNoCancel, MessageBoxImage.Question )
					!= MessageBoxResult.Yes )
				{
					return;
				} 
			}

			try
			{
				using ( MailerClient client = new MailerClient( UiCore.ServiceUsername, UiCore.ServicePassword, UiCore.ServiceUrl ) )
				{
					client.AbandonFile( file.Name );
				}

				OnDeleted();
			}
			catch ( Exception ex )
			{
				MessageBox.Show( "Error contacting service: " + ex, "Service Error", MessageBoxButton.OK, MessageBoxImage.Error );
				return;
			}
		}

		protected virtual void OnDeleted()
		{
			if ( Deleted != null )
			{
				Deleted( this, EventArgs.Empty );
			}
		}

		private void discoverableLink_Click(object sender, RoutedEventArgs e)
		{
			try
			{
				using ( MailerClient client = new MailerClient( UiCore.ServiceUsername, UiCore.ServicePassword, UiCore.ServiceUrl ) )
                {
					client.MakeFileDiscoverable( file.Name );
					file.IsDiscoverable = true;
					UpdateUi();
                }
			}
			catch ( Exception ex )
			{
				string text = "Error setting discoverability. Do you need to update the service library?\n\nDetails:\n" + ex;
				MessageBox.Show( text, "Error", MessageBoxButton.OK, MessageBoxImage.Error );
			}
		}

		private void undiscoverableLink_Click(object sender, RoutedEventArgs e)
		{
			try
			{
				using ( MailerClient client = new MailerClient( UiCore.ServiceUsername, UiCore.ServicePassword, UiCore.ServiceUrl ) )
				{
					client.MakeFileUndiscoverable( file.Name );
					file.IsDiscoverable = false;
					UpdateUi();
				}
			}
			catch ( Exception ex )
			{
				string text = "Error setting discoverability. Do you need to update the service library?\n\nDetails:\n" + ex;
				MessageBox.Show( text, "Error", MessageBoxButton.OK, MessageBoxImage.Error );
			}
		}

		private void UpdateUi()
		{
			discoverableBlock.Visibility = file.IsDiscoverable ? Visibility.Collapsed : Visibility.Visible;
			undiscoverableBlock.Visibility = file.IsDiscoverable ? Visibility.Visible : Visibility.Collapsed;
		}

		private void fileLink_Click(object sender, RoutedEventArgs e)
		{
			Process.Start( file.Url );
		}
		
	}
}